package leetcode_600;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 *@author 周杨
 *MinimumTimeDifference_539 给定一个时钟时间集合 给出其中差距最小的两者值
 *describe:先排序 然后比较队列前后 AC 42%
 *2018年9月12日 上午9:41:43
 */
public class MinimumTimeDifference_539 {
	
	public static void main(String[] args) {
		MinimumTimeDifference_539 test=new MinimumTimeDifference_539();
		System.out.println(test.help("22:08", "00:35"));
	}
	
	public int findMinDifference(List<String> timePoints) {
        Comparator<String> com=new Comparator<String>() {
			
			@Override
			public int compare(String o1, String o2) {
				char []chars1=o1.toCharArray();
				char []chars2=o2.toCharArray();
				for(int i=0;i<5;++i) {
					if(chars1[i]>chars2[i])
						return 1;
					else if(chars1[i]<chars2[i])
						return -1;
				}
				return 0;
			}
		};
		Collections.sort(timePoints, com);
		int res=Integer.MAX_VALUE;
		for(int i=1;i<timePoints.size();++i) {
			res=Math.min(help(timePoints.get(i-1), timePoints.get(i)),res);
		}
		res=Math.min(help(timePoints.get(0),timePoints.get(timePoints.size()-1)),res);
		//最后一个和第一个可能是最终值
		return res;
    }
	
	private int help(String s1,String s2) {
		int dis=(Integer.parseInt(s1.substring(0, 2))
				-Integer.parseInt(s2.substring(0, 2)))*60+
				Integer.parseInt(s1.substring(3))-
						Integer.parseInt(s2.substring(3));
		dis=Math.abs(dis);
		return Math.min(dis, 1440-dis);
	}
}
